package com.zhentao.mapcontroller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.zhentao.domain.vo.MapVo;
import com.zhentao.domain.vo.Result;
import com.zhentao.utils.MapXYUtils;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;

@RestController
@RequestMapping("/map")
@Tag(name = "地图管理")
public class Map {
    /**
     * http://lbsyun.baidu.com/apiconsole/key
     * <百度开发者>用户申请注册的key，自v2开始参数修改为“ak”，之前版本参数为“key” 申请ak
     */
    /**
     * 地理编码
     * @param address  (广东省广州市黄埔区)
     *         详细的位置信息
     * @return
     */
    @PostMapping("/addressToLongitude")
    @Operation(summary = "地址转经纬度")
    public Result<MapVo> AddressToLongitude(String address) {
        MapVo mapVo = MapXYUtils.AddressToLongitude(address);
        if (mapVo == null) {
            return Result.build(null,500,"操作失败");
        }
        return Result.build(mapVo,200,"操作成功");
    }
    @PostMapping("/getDistance")
    @Operation(summary = "获取两个经纬度之间的距离")
    public Result<MapVo> getDistance(@RequestBody MapVo mapVo) {
        double v = DistanceCalculator.getDistanceByCoordinate(mapVo);
        double distance = new BigDecimal(v).setScale(2, BigDecimal.ROUND_DOWN).doubleValue();
        mapVo.setDistance(distance);
        return Result.build(mapVo,200,"操作成功");
    }
}
